package hu.arguscab.game;

import hu.arguscab.graphic.Drawable;
import hu.arguscab.graphic.Line;
import hu.arguscab.graphic.NamedColor;
import hu.arguscab.graphic.Triangle;
import hu.arguscab.physic.PhysicProperties;
import hu.arguscab.physic.Vectors;
import java.awt.Color;


/**
 *
 * @author sharp
 */
public final class Ship implements Drawable{
	private final PhysicProperties properties;
	private final Controller controller;

	private boolean drawVectors = false;

	private ShipState state;
	private Line forceLine	= new Line();
	private Line accLine	= new Line();
	private Line vLine		= new Line();
	private final Triangle tri = new Triangle(12, 20);
	private NamedColor color;

	public Ship(PhysicProperties props, Controller c) {
		this.forceLine.setColor(1, 0, 0);
		this.accLine.setColor(0, 1, 0);
		this.vLine.setColor(0, 0, 1);
		this.tri.setColor(255, 255, 255);

		this.properties = props;
		this.state = new ShipState(this.properties, this);
		this.controller = c;
		// létezhet olyan hajó, aminek nicns controllere
		// pl a target.
		// @TODO: ez csak átmeneti... késöbb ne lehessen
		// @TODO: a this ptr nem kerülhet ki konstruktorból!
		if (c != null)
			this.controller.setMyShip(this);
		this.color = new NamedColor(Color.WHITE, "White");
	}

	public void update() {
		// @TODO: controller ne lehessen majd null
		if (controller == null) {
			return;
		}
		state.applyAction( controller.control() );
		state.update();

	}

	@Override
	public void draw() {
		forceLine.setPos(state.getPos());
		forceLine.setEndPoint(state.getForce().getX()*100, state.getForce().getY()*100);
		accLine.setPos(state.getPos());
		accLine.setEndPoint(state.getA().getX()*100, state.getA().getY()*100);
		vLine.setPos(state.getPos());
		vLine.setEndPoint(state.getV().getX()*100, state.getV().getY()*100);
		tri.setPos( state.getPos() );
		tri.setAngle((int) state.getDir().get());
		if (drawVectors) {
			forceLine.draw();
			accLine.draw();
			vLine.draw();
		}
		// @TODO: majd kivenni ezt. A GameScene target változójának null a controllere
		if ( controller != null )
			controller.draw();
		tri.draw();
	}

	public PhysicProperties getProperties() {
		return properties;
	}

	public ShipState getState() {
		return state;
	}

	public Controller getController() {
		return controller;
	}
	
	public void randomPos() {
		getState().setPos(Vectors.createRandom(GameScene.WIDTH, GameScene.HEIGHT));
	}

	public NamedColor getColor() {
		return color;
	}

	public void setColor(NamedColor color) {
		this.color = color;
		tri.setColor(color);
	}

	@Override
	public String toString() {
		return controller.toString();
	}

}
